﻿
namespace bookMaster.AssetSystem.Windows.Forms
{
	/// <summary>
	/// abstract Pedidos typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class PedidosDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<PedidosDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Pedidos _currentPedidos = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxPedidosDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxPedidosErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxPedidosBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the IdPedido property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIdPedidoDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IdCliente property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIdClienteDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the FechaPedido property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxFechaPedidoDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IdEstadoPedido property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIdEstadoPedidoDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the EstadoPedido property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxEstadoPedidoDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.Clientes> _IdClienteList;
		
		/// <summary> 
		/// The list of selectable Clientes
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Clientes> IdClienteList
		{
			get {return this._IdClienteList;}
			set 
			{
				this._IdClienteList = value;
				this.uxIdClienteDataGridViewColumn.DataSource = null;
				this.uxIdClienteDataGridViewColumn.DataSource = this._IdClienteList;
			}
		}
		
		private bool _allowNewItemInIdClienteList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Clientes
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIdClienteList
		{
			get { return _allowNewItemInIdClienteList;}
			set
			{
				this._allowNewItemInIdClienteList = value;
				this.uxIdClienteDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
				
		private Entities.TList<Entities.EstadoPedidos> _IdEstadoPedidoList;
		
		/// <summary> 
		/// The list of selectable EstadoPedidos
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.EstadoPedidos> IdEstadoPedidoList
		{
			get {return this._IdEstadoPedidoList;}
			set 
			{
				this._IdEstadoPedidoList = value;
				this.uxIdEstadoPedidoDataGridViewColumn.DataSource = null;
				this.uxIdEstadoPedidoDataGridViewColumn.DataSource = this._IdEstadoPedidoList;
			}
		}
		
		private bool _allowNewItemInIdEstadoPedidoList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of EstadoPedidos
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIdEstadoPedidoList
		{
			get { return _allowNewItemInIdEstadoPedidoList;}
			set
			{
				this._allowNewItemInIdEstadoPedidoList = value;
				this.uxIdEstadoPedidoDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Pedidos> _PedidosList;
				
		/// <summary> 
		/// The list of Pedidos to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Pedidos> PedidosList
		{
			get {return this._PedidosList;}
			set
			{
				this._PedidosList = value;
				this.uxPedidosBindingSource.DataSource = null;
				this.uxPedidosBindingSource.DataSource = value;
				this.uxPedidosDataGridView.DataSource = null;
				this.uxPedidosDataGridView.DataSource = this.uxPedidosBindingSource;				
				//this.uxPedidosBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxPedidosBindingSource_ListChanged);
				this.uxPedidosBindingSource.CurrentItemChanged += new System.EventHandler(OnPedidosBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnPedidosBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentPedidos = uxPedidosBindingSource.Current as Entities.Pedidos;
			
			if (_currentPedidos != null)
			{
				_currentPedidos.Validate();
			}
			//_Pedidos.Validate();
			OnCurrentEntityChanged();
		}

		//void uxPedidosBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.Pedidos"/> instance.
		/// </summary>
		public Entities.Pedidos SelectedPedidos
		{
			get {return this._currentPedidos;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxPedidosDataGridView.VirtualMode;}
			set
			{
				this.uxPedidosDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxPedidosDataGridView.AllowUserToAddRows;}
			set {this.uxPedidosDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxPedidosDataGridView.AllowUserToDeleteRows;}
			set {this.uxPedidosDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxPedidosDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxPedidosDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="PedidosDataGridViewBase"/> class.
		/// </summary>
		public PedidosDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxPedidosDataGridView = new System.Windows.Forms.DataGridView();
			this.uxPedidosBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxPedidosErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIdPedidoDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIdClienteDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxFechaPedidoDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIdEstadoPedidoDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxEstadoPedidoDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxIdClienteBindingSource = new ClientesBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIdClienteBindingSource)).BeginInit();
			//this.uxIdEstadoPedidoBindingSource = new EstadoPedidosBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIdEstadoPedidoBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxPedidosDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxPedidosBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxPedidosErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxPedidosErrorProvider
			// 
			this.uxPedidosErrorProvider.ContainerControl = this;
			this.uxPedidosErrorProvider.DataSource = this.uxPedidosBindingSource;						
			// 
			// uxPedidosDataGridView
			// 
			this.uxPedidosDataGridView.AutoGenerateColumns = false;
			this.uxPedidosDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxPedidosDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIdPedidoDataGridViewColumn,
		this.uxIdClienteDataGridViewColumn,
		this.uxFechaPedidoDataGridViewColumn,
		this.uxIdEstadoPedidoDataGridViewColumn,
		this.uxEstadoPedidoDataGridViewColumn			});
			this.uxPedidosDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxPedidosDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxPedidosDataGridView.Name = "uxPedidosDataGridView";
			this.uxPedidosDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxPedidosDataGridView.TabIndex = 0;	
			this.uxPedidosDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxPedidosDataGridView.EnableHeadersVisualStyles = false;
			this.uxPedidosDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnPedidosDataGridViewDataError);
			this.uxPedidosDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnPedidosDataGridViewCellValueNeeded);
			this.uxPedidosDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnPedidosDataGridViewCellValuePushed);
			
			//
			// uxIdPedidoDataGridViewColumn
			//
			this.uxIdPedidoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIdPedidoDataGridViewColumn.DataPropertyName = "IdPedido";
			this.uxIdPedidoDataGridViewColumn.HeaderText = "IdPedido";
			this.uxIdPedidoDataGridViewColumn.Name = "uxIdPedidoDataGridViewColumn";
			this.uxIdPedidoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIdPedidoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIdPedidoDataGridViewColumn.ReadOnly = true;		
			//
			// uxIdClienteDataGridViewColumn
			//
			this.uxIdClienteDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIdClienteDataGridViewColumn.DataPropertyName = "IdCliente";
			this.uxIdClienteDataGridViewColumn.HeaderText = "IdCliente";
			this.uxIdClienteDataGridViewColumn.Name = "uxIdClienteDataGridViewColumn";
			this.uxIdClienteDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIdClienteDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIdClienteDataGridViewColumn.ReadOnly = false;		
			//
			// uxFechaPedidoDataGridViewColumn
			//
			this.uxFechaPedidoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxFechaPedidoDataGridViewColumn.DataPropertyName = "FechaPedido";
			this.uxFechaPedidoDataGridViewColumn.HeaderText = "FechaPedido";
			this.uxFechaPedidoDataGridViewColumn.Name = "uxFechaPedidoDataGridViewColumn";
			this.uxFechaPedidoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxFechaPedidoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxFechaPedidoDataGridViewColumn.ReadOnly = false;		
			//
			// uxIdEstadoPedidoDataGridViewColumn
			//
			this.uxIdEstadoPedidoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIdEstadoPedidoDataGridViewColumn.DataPropertyName = "IdEstadoPedido";
			this.uxIdEstadoPedidoDataGridViewColumn.HeaderText = "IdEstadoPedido";
			this.uxIdEstadoPedidoDataGridViewColumn.Name = "uxIdEstadoPedidoDataGridViewColumn";
			this.uxIdEstadoPedidoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIdEstadoPedidoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIdEstadoPedidoDataGridViewColumn.ReadOnly = false;		
			//
			// uxEstadoPedidoDataGridViewColumn
			//
			this.uxEstadoPedidoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxEstadoPedidoDataGridViewColumn.DataPropertyName = "EstadoPedido";
			this.uxEstadoPedidoDataGridViewColumn.HeaderText = "EstadoPedido";
			this.uxEstadoPedidoDataGridViewColumn.Name = "uxEstadoPedidoDataGridViewColumn";
			this.uxEstadoPedidoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxEstadoPedidoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxEstadoPedidoDataGridViewColumn.ReadOnly = false;		
			//
			// uxIdClienteDataGridViewColumn
			//				
			this.uxIdClienteDataGridViewColumn.DisplayMember = "NombreCliente";	
			this.uxIdClienteDataGridViewColumn.ValueMember = "IdCliente";	
			this.uxIdClienteDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIdClienteDataGridViewColumn.DataSource = uxIdClienteBindingSource;				
				
			//
			// uxIdEstadoPedidoDataGridViewColumn
			//				
			this.uxIdEstadoPedidoDataGridViewColumn.DisplayMember = "EstadoPedido";	
			this.uxIdEstadoPedidoDataGridViewColumn.ValueMember = "IdEstadoPedido";	
			this.uxIdEstadoPedidoDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIdEstadoPedidoDataGridViewColumn.DataSource = uxIdEstadoPedidoBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxPedidosDataGridView);
			this.Name = "PedidosDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIdClienteBindingSource)).EndInit();
			//((System.ComponentModel.ISupportInitialize)(this.uxIdEstadoPedidoBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxPedidosErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxPedidosDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxPedidosBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				PedidosDataGridViewEventArgs args = new PedidosDataGridViewEventArgs();
				args.Pedidos = _currentPedidos;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class PedidosDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Pedidos	_Pedidos;
	
			/// <summary>
			/// the  Entities.Pedidos instance.
			/// </summary>
			public Entities.Pedidos Pedidos
			{
				get { return _Pedidos; }
				set { _Pedidos = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxPedidosDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnPedidosDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxPedidosDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnPedidosDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxPedidosDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdPedidoDataGridViewColumn":
						e.Value = PedidosList[e.RowIndex].IdPedido;
						break;
					case "uxIdClienteDataGridViewColumn":
						e.Value = PedidosList[e.RowIndex].IdCliente;
						break;
					case "uxFechaPedidoDataGridViewColumn":
						e.Value = PedidosList[e.RowIndex].FechaPedido;
						break;
					case "uxIdEstadoPedidoDataGridViewColumn":
						e.Value = PedidosList[e.RowIndex].IdEstadoPedido;
						break;
					case "uxEstadoPedidoDataGridViewColumn":
						e.Value = PedidosList[e.RowIndex].EstadoPedido;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxPedidosDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnPedidosDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxPedidosDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdPedidoDataGridViewColumn":
						PedidosList[e.RowIndex].IdPedido = (System.Int32)e.Value;
						break;
					case "uxIdClienteDataGridViewColumn":
						PedidosList[e.RowIndex].IdCliente = (System.Int32)e.Value;
						break;
					case "uxFechaPedidoDataGridViewColumn":
						PedidosList[e.RowIndex].FechaPedido = (System.DateTime?)e.Value;
						break;
					case "uxIdEstadoPedidoDataGridViewColumn":
						PedidosList[e.RowIndex].IdEstadoPedido = (System.Int32?)e.Value;
						break;
					case "uxEstadoPedidoDataGridViewColumn":
						PedidosList[e.RowIndex].EstadoPedido = (System.Int32?)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
